/**
* @module analytics
* @author: Hjava
* @since: 2018-03-02 10:33:59
*/

Object.defineProperty(exports, "__esModule", { value: true });
var log_1 = __webpack_require__(/*! ../utils/log */ "./src/core/utils/log.ts");
var worker_const_1 = __webpack_require__(/*! ./worker-const */ "./src/core/worker/worker-const.js");
var map_1 = __webpack_require__(/*! _Eutils/map */ "./src/extra/utils/map.ts");
var uuid_1 = __webpack_require__(/*! _util/uuid */ "./src/core/utils/uuid.ts");
var WorkerManager = /** @class */ (function () {
    function WorkerManager() {
        this._workerMap = new map_1.default();
    }
    WorkerManager.prototype.request = function (_a) {
        var _b = _a.workerFile, workerFile = _b === void 0 ? null : _b, _c = _a.data, data = _c === void 0 ? null : _c, _d = _a.resCallback, resCallback = _d === void 0 ? null : _d, _e = _a.timeoutTime, timeoutTime = _e === void 0 ? 0 : _e;
        try {
            if (!data || !workerFile || !resCallback) {
                log_1.default.error("WorkerManager:request params error");
                this.notifyRes({ resCallback: resCallback, code: worker_const_1.RESCODE.ERROR });
                return;
            }
            var reqId = this.createWorker({
                workerFile: workerFile,
                resCallback: resCallback,
                protoData: data,
            });
            if (!reqId || reqId === '') {
                return;
            }
            this.startTimeoutTimer(reqId, timeoutTime, resCallback);
            log_1.default.log('WorkerManager::request ' + ' reqId:' + reqId + ' timeoutTime:' + timeoutTime);
            this.sendMessage(reqId, data);
        }
        catch (error) {
            log_1.default.error("WorkerManager::request error:" + error);
            this.notifyRes({ resCallback: resCallback, code: worker_const_1.RESCODE.ERROR });
        }
    };
    WorkerManager.prototype.startTimeoutTimer = function (reqId, timeoutTime, resCallback) {
        var _this = this;
        if (timeoutTime <= 0) {
            return;
        }
        var itemInfo = this._workerMap.get(reqId);
        if (!itemInfo) {
            log_1.default.error('WorkerManager::startTimeoutTimer not found object reqId:' + reqId);
            return;
        }
        this.clearTimeoutTimer(reqId);
        itemInfo.timerId = setTimeout(function () {
            _this.onTimeout(reqId);
        }, timeoutTime);
    };
    WorkerManager.prototype.clearTimeoutTimer = function (reqId) {
        var itemInfo = this._workerMap.get(reqId);
        if (!itemInfo) {
            log_1.default.error('WorkerManager::clearTimeoutTimer not found object reqId:' + reqId);
            return;
        }
        if (itemInfo.timerId) {
            clearTimeout(itemInfo.timerId);
            itemInfo.timerId = null;
        }
    };
    WorkerManager.prototype.createWorker = function (_a) {
        var _this = this;
        var _b = _a.workerFile, workerFile = _b === void 0 ? null : _b, _c = _a.resCallback, resCallback = _c === void 0 ? null : _c, _d = _a.protoData, protoData = _d === void 0 ? null : _d;
        if (!workerFile) {
            return '';
        }
        var workerObject = null;
        if (false) {}
        else {
            if (window && window.Worker) {
                workerObject = new workerFile();
                if (!workerObject) {
                    log_1.default.error('WorkerManager::createWorker new workerFile error');
                    this.notifyRes({ resCallback: resCallback, code: worker_const_1.RESCODE.NOT_SUPPORT });
                    return '';
                }
            }
            else {
                log_1.default.error('WorkerManager::createWorker window worker not found');
                this.notifyRes({ resCallback: resCallback, code: worker_const_1.RESCODE.NOT_SUPPORT });
                return '';
            }
        }
        if (!workerObject) {
            return '';
        }
        var reqId = uuid_1.default.next();
        workerObject.onmessage = function (e) {
            _this.onMessage(e, reqId);
        };
        workerObject.onerror = function (e) {
            _this.onError(e, reqId);
        };
        this._workerMap.set(reqId, {
            worker: workerObject,
            resCallback: resCallback,
            timerId: null,
            protoData: protoData,
        });
        return reqId;
    };
    WorkerManager.prototype.onMessage = function (e, reqId) {
        if (e && e.data) {
            var reqIdFromWorker = e.data.reqId || '';
            if (reqId !== reqIdFromWorker) {
                log_1.default.error('WorkerManager::onMessage reqId error reqId:' + reqId + " reqIdFromWorker:" + reqIdFromWorker);
                return;
            }
            this.dealAndNotifyRes(reqId, e.data.code, e.data.data);
        }
        else {
            log_1.default.error('WorkerManager::onMessage not data');
        }
    };
    WorkerManager.prototype.onError = function (e, reqId) {
        console.log("WorkerManager::onError reqId:" + reqId + " error:" + e);
        this.dealAndNotifyRes(reqId, worker_const_1.RESCODE.NOT_SUPPORT, null);
    };
    WorkerManager.prototype.onTimeout = function (reqId) {
        console.log("WorkerManager::onTimeout reqId:" + reqId);
        this.dealAndNotifyRes(reqId, worker_const_1.RESCODE.TIMEOUT, null);
    };
    WorkerManager.prototype.dealAndNotifyRes = function (reqId, code, data) {
        var itemInfo = this._workerMap.get(reqId);
        if (!itemInfo || !itemInfo.worker) {
            log_1.default.error('WorkerManager::dealAndNotifyRes not found worker object reqId:' + reqId);
            return;
        }
        this.terminateWorker(reqId);
        this.notifyRes({ resCallback: itemInfo.resCallback, code: code, data: data });
    };
    WorkerManager.prototype.sendMessage = function (reqId, data) {
        if (!reqId) {
            return;
        }
        var itemInfo = this._workerMap.get(reqId);
        if (!itemInfo || !itemInfo.worker) {
            log_1.default.error('WorkerManager::sendMessage not found worker object reqId:' + reqId);
            return;
        }
        itemInfo.worker.postMessage({
            reqId: reqId,
            data: data,
        });
    };
    WorkerManager.prototype.notifyRes = function (_a) {
        var _b = _a.resCallback, resCallback = _b === void 0 ? null : _b, _c = _a.code, code = _c === void 0 ? worker_const_1.RESCODE.ERROR : _c, _d = _a.data, data = _d === void 0 ? null : _d, _e = _a.errorMessage, errorMessage = _e === void 0 ? '' : _e;
        if (!resCallback) {
            return;
        }
        resCallback({
            code: code,
            data: data,
            errorMessage: errorMessage
        });
    };
    WorkerManager.prototype.terminateWorker = function (reqId) {
        this.clearTimeoutTimer(reqId);
        var itemInfo = this._workerMap.get(reqId);
        if (!itemInfo || !itemInfo.worker) {
            log_1.default.error('WorkerManager::onMessage not found worker object reqId:' + reqId);
            return;
        }
        var workerObject = itemInfo.worker;
        workerObject.terminate();
        this._workerMap.delete(reqId);
    };
    return WorkerManager;
}());
exports.default = new WorkerManager();


//# sourceURL=webpack://MTDX/./src/core/worker/index.ts?